package com.hd.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hd.controller.base.BaseController;
import com.hd.entity.Person;
import com.hd.service.PersonService;
import com.hd.util.JsonUtil;
import com.hd.util.result.PageInfo;

@Controller
@RequestMapping("/person")
public class PersonController extends BaseController{
	@Autowired
	private PersonService personService;
	/**
	 * 添加
	 * @param person
	 * @return
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Object add(Person person) {
		personService.save(person);
		return renderSuccess("添加成功！");
	}
	/**
	 * 更新
	 * @param person
	 * @return
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Object update(Person person) {
		personService.update(person);
		return renderSuccess("编辑成功！");
	}
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Object delete(String id) {
		personService.delete(id);
		return renderSuccess("删除成功！");
	}
	
	/**
	 * 查询总数
	 * @return
	 */
	@RequestMapping("/findAll")
	@ResponseBody
	public Object findAll() {
		List<Person> personList = personService.findAll();
		return renderSuccess(personList);
	}
	
	/**
	 * 分页
	 * @param page 当前页
	 * @param PageSize
	 * @return
	 */
	@RequestMapping("/dataGrid")
	@ResponseBody
	public Object selectDataGrid(Integer page,Integer pageSize) {
		PageInfo info=new PageInfo(page,pageSize);
		Map<String,Object>condition=new HashMap<String,Object>();
		info.setCondition(condition);
		personService.selectDataGrid(info);
		System.out.println(JsonUtil.toJson(info));
		return info;
	}
	/**
	 * 分页
	 * @param page 当前页
	 * @param PageSize
	 * @return
	 */
	@RequestMapping("/page")
	@ResponseBody
	public Object page(Integer page,Integer pageSize) {
		PageInfo info=new PageInfo(page,pageSize);
		Map<String,Object>condition=new HashMap<String,Object>();
		info.setCondition(condition);
		String hql="from Person";
		List<Object>params=new ArrayList<Object>();
		int total=personService.findForSize(hql, params);
		System.out.println(total);
		List<Person>personList=personService.findList(hql, page, pageSize, params);
		info.setTotal(total);
		info.setRows(personList);
		return info;
	}

}
